// created by noellynch
// Jun 4, 2009

package com.sceneworker.editors.glslshadereditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.concurrent.Callable;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.editors.glslshadereditor.UniformRepresentations.IUniformRepresentation;

public class NewUniformPanel extends JPanel {
	
	private		GLSLShaderObjectsState		mcl_shaderState;
	
	private		JTextField					mcl_uniformName;
	private		JComboBox					mcla_uniformTypes;
	
	private		UniformsPanel 				mcl_uniformsPanel;
	
	public	NewUniformPanel(GLSLShaderObjectsState cl_state, UniformsPanel cl_uniformsPanel, final JDialog cl_par) {
		mcl_shaderState = cl_state;
		mcl_uniformsPanel = cl_uniformsPanel;
		
		setLayout(new MigLayout("inset 5"));
		
		initialiseUniformNameField();
		
		initialiseCombo();
		
		JButton		lcl_cancel = SwingComponentFactory.makeAButton("Cancel");
		lcl_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_par.setVisible(false);
			}
		});
		
		JButton		lcl_create = SwingComponentFactory.makeAButton("Create");
		lcl_create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				attemptToMakeUniform(cl_par);
			}
		});
		
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_cancel, lcl_create);
	}
	
	private	void	initialiseUniformNameField() {
		JLabel		lcl_lbl = SwingComponentFactory.makeALabelRightAligned("Uniform Name");
		add(lcl_lbl);
		
		mcl_uniformName = SwingComponentFactory.makeATextField();
		add(mcl_uniformName, "wrap");
	}
	
	private	void	initialiseCombo() {
		JLabel		lcl_lbl = SwingComponentFactory.makeALabelRightAligned("Uniform Type");
		add(lcl_lbl);
		
		DefaultComboBoxModel mcl_shaderVariableTypesModel = new DefaultComboBoxModel();
		ArrayList<IUniformRepresentation>		lcla_reps = UniformRepresentations.getAllReps();
		
		for(IUniformRepresentation lstr_key : lcla_reps) {
			mcl_shaderVariableTypesModel.addElement(lstr_key);
		}
		
		mcla_uniformTypes = SwingComponentFactory.makeAComboBox();
		mcla_uniformTypes.setModel(mcl_shaderVariableTypesModel);
		add(mcla_uniformTypes, "wrap");
	}
	
	private	void	attemptToMakeUniform(JDialog cl_par) {
		if(mcl_uniformName.getText().equals("")) {
			SimpleAlert.doAlert("You have not provided a name for the uniform.", this);
			return;
		}
		
		final	IUniformRepresentation		lcl_makeUniform = (IUniformRepresentation)mcla_uniformTypes.getSelectedItem();
		if(lcl_makeUniform == null) {
			SimpleAlert.doAlert("Cannot make uniform. Unrecognised type.", this);
			return;
		}
		
		GameTaskQueueManager.getManager().update(new Callable<Void>() {
			public Void call() throws Exception {
				if(mcl_shaderState.getUniformByName(mcl_uniformName.getText()) == null) {
					lcl_makeUniform.createUniform(mcl_shaderState, mcl_uniformName.getText());
					mcl_uniformsPanel.updateUniformsList();
					mcl_uniformsPanel.repaint();
					SimpleAlert.doAlert("Uniform created.", null);
				} else {
					SimpleAlert.doAlert("Uniform already exists.", null);
				}
				
				return null;
			}
		});
		
		cl_par.setVisible(false);
	}
	
	private	interface MakeUniform {
		void		makeShaderUniform(String str_name);
	};
}
